<?php
	require_once('paths.inc');
	require_once(INCLUDE_PATH.'constants.inc');
	require_once('page.inc');
	require_once('server_validations.inc');
	require_once(DB_PATH.'db.inc');
	
	require_once('security.inc');
    require_once('utils.inc');
	
	session_start();
	
    select_language();
    
	if (isLoggedIn()) 
		check_permissions(CT_PERM_EDIT_CLIENT);
  		
	if (isset($_POST['post_client'])) {
		$db = db_connect();
		
		// Informatii client
		$nume_firma = $_POST['nume_firma'];
		$cf = $_POST['cf'];	
		$rc = $_POST['rc'];	
		$tel_fix = $_POST['tel_fix'];	
		$tel_fax = $_POST['tel_fax'];	
		$tel_mobil = $_POST['tel_mobil'];	
		$email_firma = $_POST['email_firma'];	
		$web = $_POST['web'];	
        $max_useri = $_POST['max_useri'];    
		
		// Informatii user
		$username = $_POST['username'];	
		$password = $_POST['password'];	
		$password2 = $_POST['password2'];	
		$nume = $_POST['nume'];	
		$client_cnp = $_POST['client_cnp'];	
		$client_serie_ci = $_POST['client_serie_ci'];	
		$prenume = $_POST['prenume'];	
		$email = $_POST['email'];	
		
		// informatii factura
		$factura_nume = $_POST['factura_nume'];	
		$factura_cnp = $_POST['factura_cnp'];	
		$factura_serie_ci = $_POST['factura_serie_ci'];	
		
		// adresa
		$adresa = $_POST['adresa'];	
		$oras = $_POST['oras'];	
		$judet = $_POST['judet'];	
		$cod_postal = $_POST['cod_postal'];	
		$tara = $_POST['tara'];	
		
		//tip abonament si ciclu al platii
        $tip_abon = $_POST['tip_abonament'];
        $client_reducere = CT_ABONAMENT_NEREDUS;
		$client_reducere = $_POST['client_reducere'];
		$ciclu_plata = $_POST['ciclu_plata'];
		
		//server side validations
		$_SESSION['wrong_ids'] = array();
		
		$conts = 0;
        for ($i = 0; $i < count($_SESSION['client']->conturi); $i++)
            if ($_SESSION['client']->conturi[$i]->status != CT_CONT_DELETED) {
                $conts++;
                break;    
            }        
        if ($conts < 1) {
            $_SESSION['wrong_ids']['conts'] = 1;
            $mesaj_aditional = LANG_Trebuie_sa_adaugati_cel_putin_un_cont.'<br />';
        }
		
        try {
            $nr_users = Client::getUsersCount($db, $_SESSION['user_logat']->id_client);
        }
        catch (QueryLoadClientException $e) {
            redirectOnError(CT_SHOW_COMENZI_LOAD_COMANDA);
        }
 
	    if ($tip_abon == CT_ABONAMENT_TIP1) {
		    $max_useri = CT_USERI_ABON_TIP_1;
	    }
	    else
		    if ($tip_abon == CT_ABONAMENT_TIP2) {
			    $max_useri = CT_USERI_ABON_TIP_2;
		    }
		    else
			    if ($tip_abon == CT_ABONAMENT_TIP3) {
				    $max_useri = CT_USERI_ABON_TIP_3;
			    }
			    else {
				    $_SESSION['wrong_ids']['tip_abonament'] = 1;
			    }

        if ($client_reducere != CT_ABONAMENT_REDUS && $client_reducere != CT_ABONAMENT_NEREDUS)
            $_SESSION['wrong_ids']['client_reducere'] = 1;
        $_SESSION['client']->client_reducere = $client_reducere;

        if ($max_useri < $nr_users && isLoggedIn()) {
            $_SESSION['wrong_ids']['tip_abonament'] = 1;
            $mesaj_aditional .= LANG_Trebuie_sa_mai_stergeti_din_useri_pt_acest_abonament . '<br />';
        }
        $_SESSION['client']->tip_abon = $tip_abon;
        
	    if ($ciclu_plata == CT_CICLU_PLATA_1) 
			$ciclu_plata = 0;
	    if ($ciclu_plata == CT_CICLU_PLATA_2) 
			$ciclu_plata = 1;
	    if ($ciclu_plata == CT_CICLU_PLATA_3) 
			$ciclu_plata = 2;
	    
        if ($ciclu_plata < 0 || $ciclu_plata > 2)
		    $_SESSION['wrong_ids']['ciclu_plata'] = 1;

        $_SESSION['client']->ciclu_plata = $ciclu_plata;
			
        $numeFirmaUnic = true;     
        if (!isLoggedIn()) {        
            $query = "SELECT nume FROM client WHERE nume LIKE '%$nume_firma%'";
            if (($result = $db->query($query)) !== FALSE) {    
                $i = 0;
                while ($data = $result->fetch(PDO::FETCH_ASSOC))
                    $i++;                      
                if ($i > 0)
                    $numeFirmaUnic = false;
            }
            //else ----------------BUG!!!!
            //    $numeFirmaUnic = false;
        }
        
		if (!validPrintable($nume_firma, 1, CT_MAX_NUME_FIRMA_LENGTH) || !$numeFirmaUnic) {
			$_SESSION['wrong_ids']['nume_firma'] = 1;
        }

		$_SESSION['client']->nume = $nume_firma;                                   
        
             
		if (!validPrintable($cf, 1, CT_MAX_CF_LENGTH))
			$_SESSION['wrong_ids']['cf'] = 1;		

        if (!isLoggedIn())
			$_SESSION['client']->cf = $cf;			
			                                         
                                                     
		if (!validPrintable($rc, 1, CT_MAX_RC_LENGTH))
			$_SESSION['wrong_ids']['rc'] = 1;		

        if (!isLoggedIn())
			$_SESSION['client']->rc = $rc;
					
		if (!validPhone($tel_fix, false))
			$_SESSION['wrong_ids']['tel_fix'] = 1;		

		$_SESSION['client']->tel_fix = $tel_fix;
			
			
		if (!validPhone($tel_fax, false))
			$_SESSION['wrong_ids']['tel_fax'] = 1;		

		$_SESSION['client']->tel_fax = $tel_fax;
			
			
		if (!validPhone($tel_mobil, false))
			$_SESSION['wrong_ids']['tel_mobil'] = 1;		

		$_SESSION['client']->tel_mobil = $tel_mobil;

        if ($tel_fix == '' && $tel_mobil == '') {
            $_SESSION['wrong_ids']['tel_fix'] = 1;
            $_SESSION['wrong_ids']['tel_mobil'] = 1;    
        }
			
		if (!validMail($email_firma, 1, CT_MAX_MAIL_LENGTH))
			$_SESSION['wrong_ids']['email_firma'] = 1;		

		$_SESSION['client']->email = $email_firma;
			
			
		if (!validSite($web, 0, CT_MAX_SITE_LENGTH))
			$_SESSION['wrong_ids']['web'] = 1;		

		$_SESSION['client']->web = $web;
			
        if (!isLoggedIn()) {    			
		    $aux = filter_var($max_useri, FILTER_VALIDATE_INT);
		    if ($aux === false) {
			    $_SESSION['wrong_ids']['max_useri'] = 1;
		    }
			$_SESSION['client']->max_useri = (int)$max_useri;
        }    
			
        if (!isLoggedIn()) {    
		    $aux = filter_var($tip_abon, FILTER_VALIDATE_INT);
		    if ($aux === false) {
			    $_SESSION['wrong_ids']['tip_abon'] = 1;
		    }
			$_SESSION['client']->tip_abon = $tip_abon;
        }    
			
		if (!isLoggedIn()) {	
		    $aux = filter_var($ciclu_plata, FILTER_VALIDATE_INT);
		    if ($aux === false) {
			    $_SESSION['wrong_ids']['ciclu_plata'] = 1;
		    }
			$_SESSION['client']->ciclu_plata = (int)$ciclu_plata;
        }    
		
		$usUnic = true;     
        if (!isLoggedIn()) {        
            $query = "SELECT username FROM user WHERE username LIKE '%$username%'";
            if (($result = $db->query($query)) != FALSE) {    
                $i = 0;
                while ($data = $result->fetch(PDO::FETCH_ASSOC))
                    $i++;                      
                if ($i > 0)
                    $usUnic = false;
            }
            else
                $usUnic = false;
        }
        // verificam si sa nu aibe spatii
        if (!validPrintable($username, 1, CT_MAX_USERNAME_LENGTH) || !$usUnic || strstr($username, " ")) {
			$_SESSION['wrong_ids']['username'] = 1;
		}

        if (!isLoggedIn())
			$_SESSION['user']->username = $username;
			
		if ($_SESSION['user']->id_user == CT_NOT_SAVED_YET) {
			if (!validPrintable($password, 1, CT_MAX_PASSWORD_LENGTH)) {
				$_SESSION['wrong_ids']['password'] = 1;
			}
			$_SESSION['user']->password = $password;
			$_SESSION['user']->nonCryptedPassword = $password;
		}
			
		if (!validPrintable($nume, 1, CT_MAX_NUME_LENGTH)) {
			$_SESSION['wrong_ids']['nume'] = 1;
		}

		$_SESSION['user']->nume = $nume;
			
			
		if (!validPrintable($prenume, 1, CT_MAX_PRENUME_LENGTH)) {
			$_SESSION['wrong_ids']['prenume'] = 1;
		}

		$_SESSION['user']->prenume = $prenume;				
		
		// VERIFICARE UNICITATE EMAIL USER
		$emailUnic = true;                     
        $query = "SELECT email FROM user WHERE email LIKE '%$email%'";
        if (($result = $db->query($query)) != FALSE) {    
            $i = 0;
            while ($data = $result->fetch(PDO::FETCH_ASSOC))
                $i++;
                                      
            if ($i > 0 && !isLoggedIn()) 
                $emailUnic = false;
            else
                if (isLoggedIn() && $i >1)
                    $emailUnic = false;
        }
            
		if (!validMail($email, 1, CT_MAX_MAIL_LENGTH) || !$emailUnic) {
			$_SESSION['wrong_ids']['email'] = 1;
		}

		$_SESSION['user']->email = $email;
			
			
		if (!validPrintable($client_cnp, 1, CT_MAX_TEXT_CNP)) {
			$_SESSION['wrong_ids']['client_cnp'] = 1;
		}

		$_SESSION['client']->client_cnp = $client_cnp;
			
			
		if (!validPrintable($client_serie_ci, 1, CT_MAX_TEXT_SERIE_CI)) {
			$_SESSION['wrong_ids']['client_serie_ci'] = 1;
		}

		$_SESSION['client']->client_serie_ci = $client_serie_ci;
			
		// informatii factura
		if (!validPrintable($factura_nume, 1, CT_MAX_NUME_LENGTH)) {
			$_SESSION['wrong_ids']['factura_nume'] = 1;
		}

		$_SESSION['client']->factura_nume = $factura_nume;
			
		
		if (!validPrintable($factura_cnp, 1, CT_MAX_TEXT_CNP)) {
			$_SESSION['wrong_ids']['factura_cnp'] = 1;
		}

		$_SESSION['client']->factura_cnp = $factura_cnp;
			
			
		if (!validPrintable($factura_serie_ci, 1, CT_MAX_TEXT_SERIE_CI)) {
			$_SESSION['wrong_ids']['factura_serie_ci'] = 1;
		}
		$_SESSION['client']->factura_serie_ci = $factura_serie_ci;	
		
		if (!validPrintable($adresa, 1, CT_MAX_ADRESA_LENGTH)) {
			$_SESSION['wrong_ids']['adresa'] = 1;
		}
 
		$_SESSION['client']->adresa->adresa = $adresa;
			
			
		if (!validPrintable($oras, 1, CT_MAX_ORAS_LENGTH)) {
			$_SESSION['wrong_ids']['oras'] = 1;
		}

		$_SESSION['client']->adresa->oras = $oras;
			
			
		if (!validPrintable($judet, 1, CT_MAX_JUDET_LENGTH)) {
			$_SESSION['wrong_ids']['judet'] = 1;
		}
 
		$_SESSION['client']->adresa->judet = $judet;
		
			
		if (!validPrintable($cod_postal, 1, CT_MAX_COD_POSTAL_LENGTH)) {
			$_SESSION['wrong_ids']['cod_postal'] = 1;
		}
 
		$_SESSION['client']->adresa->cod_postal = $cod_postal;
					
			
		if (!validPrintable($tara, 1, CT_MAX_TARA_LENGTH)) {
			$_SESSION['wrong_ids']['tara'] = 1;
		}

		$_SESSION['client']->adresa->tara = $tara;
			
				
		if (count($_SESSION['wrong_ids']) > 0) {
			notice_msg($mesaj_aditional . LANG_Campurile_rosii_trebuiesc_completate_corect, 'add_client.php');			
		}
				
		if ($_SESSION['client']->id_client == CT_NOT_SAVED_YET) { 
			//new firma
			try {
				$_SESSION['user']->change_pass = CT_NOT_CHANGE_PASSWORD;
                // set the language for the user - based on the language he selected on the presentation site
                // TODO: de facut chestia asta automat pt mai multe limbi...
                if ($_SESSION['language'] == 'romana')
                    $_SESSION['user']->language = 0;
                else
                    $_SESSION['user']->language = 1;
                // Dam userului administrator toate drepturile!!!
                $_SESSION['user']->tip = CT_PERM_ADMIN_ABONAMENT | CT_PERM_COMENZI | CT_PERM_COMENZI_DUPA_CONFIRMARE 
                                        | CT_PERM_EDIT_CLIENT | CT_PERM_EDIT_USERS | CT_PERM_FIRME | CT_PERM_PLATI | CT_PERM_STATISTICI;
                    
				$id_client = $_SESSION['client']->queryInsert($db, $_SESSION['user']);
				$_SESSION['client'] = Client::queryLoad($db, $id_client);
				
                $nonCryptedPassword = $_SESSION['user']->nonCryptedPassword;
                //because of this reload, the non crypted password is lost...., so we save it in $nonCryptedPassword
                
                create_user_folders($_SESSION['client']->get_admin_user_id());
                
				$_SESSION['user'] = User::queryLoad($db, $_SESSION['client']->get_admin_user_id());
				$_SESSION['user']->id_client = $id_client;	
				$_SESSION['user']->queryUpdate($db);
				$_SESSION['user']->nonCryptedPassword = $nonCryptedPassword;
                unset($nonCryptedPassword);
                sendEmailForNewClient(LANG_Body_New_Client_Email, LANG_Body_New_User_From_Client_Email, LANG_Signature_Email, $_SESSION['client'], $_SESSION['user']);
                
				unset($_SESSION['client']);
				unset($_SESSION['user']);
				$_SESSION = array();
                notice_msg('', "add_client_mesaj.php");
			}
			catch (QueryInsertClientException $e) {
				unset($_SESSION['client']);
				unset($_SESSION['user']);
				redirectOnError(CT_ADD_EDIT_CLIENT_INSERT_CLIENT);
			}
			catch (QueryWrongVersionException $e) {
				unset($_SESSION['client']);
				unset($_SESSION['user']);
				notice_msg(LANG_Datele_pe_care_lucrati_au_fost_modificate, 'add_client.php');
			}
		}
		else {
			//edit client
			try {
				$_SESSION['client']->queryUpdate($db);
				$_SESSION['user']->queryUpdate($db);
                
                //sendEmailForNewClient($_SESSION['client'], $_SESSION['user']);
                
				notice_msg(LANG_Clientul_a_fost_salvat, "add_client.php");
			}
			catch (QueryUpdateClientException $e) {				
				redirectOnError(CT_ADD_EDIT_CLIENT_UPDATE_CLIENT);
			}
			catch (QueryUpdateUserException $e) {
				redirectOnError(CT_ADD_EDIT_CLIENT_UPDATE_USER);				
			}
			catch (QueryWrongVersionException $e) {
				notice_msg(LANG_Datele_pe_care_lucrati_au_fost_modificate, 'add_client.php');
			}
		}				
	}
	else {        
		if (isset($_SESSION['wrong_ids'])) {            
			if (count($_SESSION['wrong_ids']) > 0) {
				$wrong_ids = $_SESSION['wrong_ids'];				
				unset($_SESSION['wrong_ids']);
			}
		}
		else {                                      
			if (isLoggedIn()) {
				//edit                
				$id = $_SESSION['user_logat']->id_client;	
				$db = db_connect();
				try {
					$_SESSION['client'] = Client::queryLoad($db, $id);
					$_SESSION['user'] = User::queryLoad($db, $_SESSION['client']->admin_user_id);
				}
				catch (QueryLoadUserException $e) {
					redirectOnError(CT_ADD_EDIT_CLIENT_LOAD_USER);
				}
				catch (QueryLoadClientException $e) {
					redirectOnError(CT_ADD_EDIT_CLIENT_LOAD_CLIENT);
				}
				catch (QueryLoadAddressException $e) {
					redirectOnError(CT_ADD_EDIT_CLIENT_LOAD_ADDRESS);
				}
			}
			else {                
				//add new
				$db = db_connect();
				$_SESSION['client'] = new Client('', '', '', 0, 0, 0, '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
				$_SESSION['user'] = new User('', '', '', '', '', '');
				$_SESSION['user']->tip = CT_PERM_COMENZI | CT_PERM_FIRME | CT_PERM_PLATI | CT_PERM_STATISTICI 
										| CT_PERM_EDIT_USERS | CT_PERM_EDIT_CLIENT | CT_PERM_ADMIN_ABONAMENT;
			}
		}
		
	if (isset($_GET['lang'])) {
		if ($_GET['lang'] == "ro") 
			$_SESSION['language'] = "romana";
		elseif ($_GET['lang'] == "en") 
			$_SESSION['language'] = "english";
    }
        
	if (isLoggedIn()) 
        html_header("LANG_Edit_Client", null, "add_client", "yes");	
    else {        
        html_header("LANG_Add_Client", null, null, null, "add_client.php");
    }

    if (isLoggedIn()) {
        $linkuri = array(LANG_Home => "home.php", LANG_titlu_editeaza_client => "");
        titlu_pagina($linkuri);
    }
	else {        
	    $linkuri = array(LANG_Home => "index.php", LANG_Add_Client => ""); 
        titlu_pagina($linkuri);
	}
	output_notice_msg();           
?>
<script type="text/javascript">
    function checkConts() {
				if (document.getElementById('contsCount').innerHTML == '0') {
                    alert(LANG_minim_un_cont);
                    return false;
                }
                else
                    return true; 
            }
    
    function validateForm() {   
    
        //check for passwork
        if (document.getElementById('password') == null) {
            //nu verific parola
        }
        else {
            //verific ca parola sa fie si sa fie identica
            var parola_ok = validatePrintable(document.add_client.password.value, '<?php echo LANG_Password; ?>', true, 5) &&
                            validatePrintable(document.add_client.password2.value, '<?php echo LANG_PASSWORD_AGAIN; ?>', true, 5);
            if (parola_ok == false)
                return false;
            if (document.add_client.password.value != document.add_client.password2.value) {
                alert(LANG_Pass_No_Match);
                return false;
            }
        }
             
        var primele_trei = validatePrintable(document.add_client.nume_firma.value, '<?php echo LANG_NUME_FIRMA; ?>', true, 3) &&
                            validatePrintable(document.add_client.cf.value, '<?php echo LANG_CF; ?>', true, 5) &&
                            validatePrintable(document.add_client.rc.value, '<?php echo LANG_RC; ?>', true, 5);
        if (primele_trei == false)
            return false;
        
        if (document.add_client.tel_fix.value.length == 0 && 
                document.add_client.tel_fax.value.length == 0 && 
                document.add_client.tel_mobil.value.length == 0) {
                alert(LANG_At_Least_0_Phone_Mandatory);
                return false;
        }
                
        var urmatoarele = validatePhone(document.add_client.tel_fix.value, false, '<?php echo LANG_TEL_FIX; ?>') &&
                        validatePhone(document.add_client.tel_fax.value, false, '<?php echo LANG_TEL_FAX; ?>') &&                    
                        validatePhone(document.add_client.tel_mobil.value, false, '<?php echo LANG_TEL_MOBIL; ?>') &&
                        validateMail(document.add_client.email_firma.value, true) &&
                        validatePrintable(document.add_client.username.value, '<?php echo LANG_Username; ?>', true, 5) &&
                        
                        validatePrintable(document.add_client.prenume.value, '<?php echo LANG_PRENUME; ?>', true, 2) &&
                        validatePrintable(document.add_client.nume.value, '<?php echo LANG_nume; ?>', true, 2) &&
                        validateMail(document.add_client.email.value, true) &&
                        validatePrintable(document.add_client.adresa.value, '<?php echo LANG_ADRESA; ?>', true, 5) &&
                        validatePrintable(document.add_client.oras.value, '<?php echo LANG_ORAS; ?>', true, 3) &&
                        validatePrintable(document.add_client.judet.value, '<?php echo LANG_JUDET; ?>', true, 2) &&
                        validatePrintable(document.add_client.cod_postal.value, '<?php echo LANG_COD_POSTAL; ?>', true, 1) &&
                        validatePrintable(document.add_client.tara.value, '<?php echo LANG_TARA; ?>', true, 2) && 
                        checkConts();
                        
        return urmatoarele;        
    }
</script>
<?php 
    output_error_msg(); 
    if (isLoggedIn())
        output_extra_text(LANG_EXTRA_EDIT_CLIENT);
    else 
        output_extra_text(LANG_EXTRA_ADD_CLIENT, true);
?>
	<div class="container">
	<?php
		if (!isset($_SESSION['user_logat'])) {
			echo "<input onclick=\"javascript: document.location='print_model_contract.php';\" name=\"tipar_contract\" type=\"button\" value=\"" . LANG_Print_model_contract . "\" />";
			echo '<br />';
			echo '<br />';
		}
	?>
	<table align="left" cellpadding="0" cellspacing="0" border="0">
		<form name="add_client" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return (validateForm());">
		<tr>
			<td class="capTabel" rowspan="2" valign="top">
				<table id="wrapper2">
                    <thead>
                        <tr>
                            <th colspan="2"><?php echo LANG_COMPANY_INFORMATION; ?></th>
                        </tr>
                    </thead>
				    <tr>
				        <td <?php check_field('nume_firma'); ?>><?php echo LANG_COMPANY_NAME . " " . CT_OBLIGATORIU; ?></td>
				        <td><input <?php echo show_tooltip(LANG_Nume_firma_tooltip); ?> type="text" size="<?php echo CT_MAX_COMPANY_NAME;?>" maxlength="<?php echo CT_MAX_COMPANY_NAME ;?>" name="nume_firma"
				        		value="<?php echo $_SESSION['client']->nume ?>"<?php if (isLoggedIn()) echo 'readonly="readonly"' ?>></td>
                    </tr>
				    <tr>
				        <td><?php echo LANG_CF . " " . CT_OBLIGATORIU; ?></td>
				        <td><input <?php echo show_tooltip(LANG_cf_tooltip); ?> <?php check_field('cf'); ?> type="text" size="15" maxlength="<?php echo CT_MAX_CF_LENGTH;?>" name="cf"
				        		value="<?php echo $_SESSION['client']->cf ?>" <?php if (isLoggedIn()) echo 'readonly="readonly"' ?>></td>
                    </tr>
				    <tr>
				        <td <?php check_field('rc'); ?>><?php echo LANG_RC . " " . CT_OBLIGATORIU; ?></td>
				        <td><input <?php echo show_tooltip(LANG_rc_tooltip); ?> type="text" size="15" maxlength="<?php echo CT_MAX_RC_LENGTH;?>" name="rc"
				        		value="<?php echo $_SESSION['client']->rc ?>" <?php if (isLoggedIn()) echo 'readonly="readonly"' ?>></td>
                    </tr>
				    <tr>
				        <td <?php check_field('tel_fix'); ?>><?php echo LANG_TEL_FIX. " " . CT_OBLIGATORIU; ?></td>
				        <td><input <?php echo show_tooltip(LANG_tel_fix_tooltip); ?> type="text" class="input_tel" maxlength="<?php echo CT_MAX_TEL; ?>" name="tel_fix"
				        		value="<?php echo $_SESSION['client']->tel_fix ?>"></td>
                    </tr>
				    <tr>
				        <td <?php check_field('tel_fax'); ?>><?php echo LANG_TEL_FAX; ?></td>
				        <td><input <?php echo show_tooltip(LANG_tel_fax_tooltip); ?> type="text" class="input_tel" maxlength="<?php echo CT_MAX_TEL; ?>" name="tel_fax"
				        		value="<?php echo $_SESSION['client']->tel_fax ?>"></td>
                    </tr>
				    <tr>
				        <td <?php check_field('tel_mobil'); ?>><?php echo LANG_TEL_MOBIL. " " . CT_OBLIGATORIU; ?></td>
				        <td><input <?php echo show_tooltip(LANG_tel_mobil_tooltip); ?> type="text" class="input_tel" maxlength="<?php echo CT_MAX_TEL; ?>" name="tel_mobil"
				        		value="<?php echo $_SESSION['client']->tel_mobil ?>"></td>
                    </tr>
				    <tr>
				        <td <?php check_field('email_firma'); ?> align="left"><?php echo LANG_EMAIL . " " . CT_OBLIGATORIU; ?></td>
				        <td><input <?php echo show_tooltip(LANG_email_tooltip); ?> type="text" class="input_email" maxlength="<?php echo CT_MAX_MAIL_LENGTH; ?>" name="email_firma"
				        		value="<?php echo $_SESSION['client']->email ?>"></td>
                    </tr>
				    <tr>
				        <td <?php check_field('web'); ?> align="left"><?php echo LANG_WEB; ?></td>
				        <td><input type="text" <?php echo show_tooltip(LANG_web_tooltip); ?> class="input_web" maxlength=<?php echo CT_MAX_SITE_LENGTH; ?> name="web"
				        		value="<?php echo $_SESSION['client']->web ?>"></td>
				    </tr>
                </table>			    
            </td>
            <td width="20"></td>
            <td valign="top">
                <table id="wrapper2" width="280" class="capTabel">
                    <thead>
                        <tr>
                            <th colspan="2"><?php echo LANG_USER_INFORMATION; ?></th>
                        </tr>
                    </thead>
                    <tr>
                        <td  <?php check_field('username'); ?>align="left"><?php echo LANG_Username . " " . CT_OBLIGATORIU; ?></td>
                        <td><input <?php echo show_tooltip(LANG_Username_tooltip); ?> type="text" size="15" maxlength="<?php echo CT_MAX_USERNAME_LENGTH;?>" name="username" 
                                  value="<?php echo $_SESSION['user']->username ?>" <?php if (isLoggedIn()) echo 'readonly="readonly"' ?>></td>
                    </tr>
                    <?php if ($_SESSION['user']->id_user == CT_NOT_SAVED_YET) { ?>
                        <tr>
                            <td <?php check_field('password'); ?>align="left"><?php echo LANG_Password . " " . CT_OBLIGATORIU; ?></td>
                                <td><input <?php echo show_tooltip(LANG_Password_tooltip); ?> id="password" type="password" size="15" maxlength="<?php echo CT_MAX_PASSWORD_LENGTH;?>" name="password"
                                        value="<?php echo $_SESSION['user']->password ?>"></td>
                        </tr>
                        <tr>
                            <td <?php check_field('password2'); ?>align="left"><?php echo LANG_PASSWORD_AGAIN . " " . CT_OBLIGATORIU; ?></td>
                            <td><input <?php echo show_tooltip(LANG_PASSWORD_AGAIN); ?> id="password2" type="password" size="15" maxlength="<?php echo CT_MAX_PASSWORD_LENGTH;?>" name="password2"
                                        value="<?php echo $_SESSION['user']->password ?>"></td>
                        </tr>
                    <?php } ?>
                    <tr>
                        <td  <?php check_field('prenume'); ?>align="left"><?php echo LANG_PRENUME . " " . CT_OBLIGATORIU; ?></td>
                        <td><input <?php echo show_tooltip(LANG_PRENUME_tooltip); ?> type="text" size="15" maxlength="<?php echo CT_MAX_PRENUME_LENGTH;?>" name="prenume" 
                                  value="<?php echo $_SESSION['user']->prenume ?>"></td>
                    </tr>
                    <tr>
                        <td  <?php check_field('nume'); ?>align="left"><?php echo LANG_NUME . " " . CT_OBLIGATORIU; ?></td>
                        <td><input <?php echo show_tooltip(LANG_Nume_tooltip); ?> type="text" size="15" maxlength="<?php echo CT_MAX_NUME_LENGTH;?>" name="nume" 
                                  value="<?php echo $_SESSION['user']->nume ?>"></td>
                    </tr>
                    <tr>
                        <td  <?php check_field('email'); ?>align="left"><?php echo LANG_EMAIL . " " . CT_OBLIGATORIU; ?></td>
                        <td><input type="text" <?php echo show_tooltip(LANG_email_tooltip); ?> class="input_email" maxlength="<?php echo CT_MAX_MAIL_LENGTH; ?>" name="email"
                                value="<?php echo $_SESSION['user']->email ?>"></td>
                    </tr>
                    <tr>
                        <td  <?php check_field('client_cnp'); ?>align="left"><?php echo LANG_CNP . " " . CT_OBLIGATORIU; ?></td>
                        <td><input type="text" <?php echo show_tooltip(LANG_CNP_tooltip); ?> maxlength="<?php echo CT_MAX_TEXT_CNP; ?>" name="client_cnp"
                                value="<?php echo $_SESSION['client']->client_cnp ?>"></td>
                    </tr>
                    <tr>
                        <td  <?php check_field('client_serie_ci'); ?>align="left"><?php echo LANG_Serie_CI . " " . CT_OBLIGATORIU; ?></td>
                        <td><input type="text" <?php echo show_tooltip(LANG_Serie_CI_tooltip); ?> maxlength="<?php echo CT_MAX_TEXT_SERIE_CI; ?>" name="client_serie_ci"
                                value="<?php echo $_SESSION['client']->client_serie_ci ?>"></td>
                    </tr>
                </table>
                <br />
                <table id="wrapper2" class="capTabel" width="280">
                    <thead>
                        <tr>
                            <th colspan="4"><?php echo LANG_DETALII_ABONAMENT; ?></th>
                        </tr>
                    </thead>
                    <tr>
                        <td <?php check_field('tip_abonament'); ?>><?php echo LANG_TIP_ABONAMENT; ?></td>
                        <td>
                            <select name="tip_abonament" class="select_tip_abonament_ciclu_plata" id="tip_abonament" 
                                onchange="javascript: show_pret_per_month(this.value);" <?php //if (isLoggedIn()) echo 'disabled="disabled"' 
                                                                                        ?>>
                                  <option value="0" <?php if ($_SESSION['client']->tip_abon == 0) echo 'selected="selected"' ?>><?php echo LANG_5_users; ?></option>
                                  <option value="1" <?php if ($_SESSION['client']->tip_abon == 1) echo 'selected="selected"' ?>><?php echo LANG_10_users; ?></option>
                                  <option value="2" <?php if ($_SESSION['client']->tip_abon == 2) echo 'selected="selected"' ?>><?php echo LANG_Unlimited_users; ?></option>                      
                            </select>                          
                        </td>
                    </tr>
                    <tr>
                        <td <?php check_field('ciclu_plata'); ?>><?php echo LANG_CICLU_PLATA; ?></td>
                        <td>
                              <select name="ciclu_plata" class="select_tip_abonament_ciclu_plata" id="ciclu_plata" onchange="javascript: show_pret_total(this.value);" <?php 
                                                                                                                                                               // if (isLoggedIn()) echo 'disabled="disabled"'                                                                                                                                                                       ?>>
                                  <option value="0" <?php if ($_SESSION['client']->ciclu_plata == 0) echo 'selected="selected"' ?>><?php echo LANG_3_months; ?></option>
                                  <option value="1" <?php if ($_SESSION['client']->ciclu_plata == 1) echo 'selected="selected"' ?>><?php echo LANG_6_months; ?></option>
                                  <option value="2" <?php if ($_SESSION['client']->ciclu_plata == 2) echo 'selected="selected"' ?>><?php echo LANG_1_year; ?></option>                      
                              </select>
                        </td>                        
                    </tr>
                    <tr>
                    	<?php
							$tip_abon_cl = constant('CT_ABONAMENT_TIP_' . ($_SESSION['client']->tip_abon + 1));	
							if ($_SESSION['client']->ciclu_plata == 0) $ciclu_plata_cl = 3;
							if ($_SESSION['client']->ciclu_plata == 1) $ciclu_plata_cl = 6;
							if ($_SESSION['client']->ciclu_plata == 2) $ciclu_plata_cl = 12;
							$total_pret = $ciclu_plata_cl * $tip_abon_cl;
							
							$reducere = 0;
							if ($ciclu_plata_cl == 6) $reducere = $tip_abon_cl / 2;
							if ($ciclu_plata_cl == 12) $reducere = $tip_abon_cl * 2;
							
                    	?>
                    	<td colspan="2">    
                    	 <?php echo LANG_Pret_pe_luna; ?>: <b><span id="pret_per_month"></span></b><br />                          
                         <?php echo LANG_Total_pret_abonament; ?>: <b><span id="pret_total"></span></b><br />
                         <?php echo LANG_Reducere; ?>: <b><span id="total_reducere"></span></b><br />
                         <?php echo LANG_Pret_abonament_cu_reducere; ?>: <b><span id="pret_total_reducere"></b></span>
                         <br />
                         <?php
                            $text = '';
                            if ($_SESSION['client']->client_reducere == 1)
                                $text = 'checked = "checked"';
                            echo '<input type="checkbox" id="client_reducere" name="client_reducere" value="1" '.$text.'/>';
                            echo LANG_Client_bursa_transport;
                         ?>
                        </td>	                                                                         
                    </tr>
                </table>
            </td>   
            <td width="20"></td>       
            <td valign="top">
				<table id="wrapper2" width="280" class="capTabel">
                    <thead>
                        <tr>
                            <th colspan="6"><?php echo LANG_Informatii_factura; ?></th>
                        </tr>
                    </thead>
                    <tr>
                        <td  <?php check_field('factura_nume'); ?>align="left"><?php echo LANG_NUME . " " . CT_OBLIGATORIU; ?></td>
                        <td><input <?php echo show_tooltip(LANG_Factura_nume_tooltip); ?> type="text" maxlength="<?php echo CT_MAX_NUME_LENGTH;?>" name="factura_nume" 
                                  value="<?php echo $_SESSION['client']->factura_nume ?>"></td>
                    </tr>
                    <tr>
                        <td  <?php check_field('factura_cnp'); ?>align="left"><?php echo LANG_CNP . " " . CT_OBLIGATORIU; ?></td>
                        <td><input <?php echo show_tooltip(LANG_Factura_CNP_tooltip); ?> type="text" maxlength="<?php echo CT_MAX_TEXT_CNP; ?>" name="factura_cnp"
                                value="<?php echo $_SESSION['client']->factura_cnp ?>"></td>
                    </tr>
                    <tr>
                        <td  <?php check_field('factura_serie_ci'); ?>align="left"><?php echo LANG_Serie_CI . " " . CT_OBLIGATORIU; ?></td>
                        <td><input <?php echo show_tooltip(LANG_Factura_CI_tooltip); ?> type="text" maxlength="<?php echo CT_MAX_TEXT_SERIE_CI; ?>" name="factura_serie_ci"
                                value="<?php echo $_SESSION['client']->factura_serie_ci ?>"></td>
                    </tr>
                </table>
                <br />
                <table class="capTabel" id="wrapper2" width="280">
                    <thead>
                        <tr>
                            <th colspan="2"><?php echo LANG_Adresa_firma; ?></th>
                        </tr>
                    </thead>
                    <tr>
                        <td <?php check_field('adresa'); ?> align="left">
                            <label for="adresa"><?php echo LANG_ADRESA . " " . CT_OBLIGATORIU; ?></label>
                        </td>
                        <td>
                            <textarea <?php echo show_tooltip(LANG_Adresa_tooltip); ?> id="adresa" name="adresa" cols="30" 
                                rows="2"><?php echo $_SESSION['client']->adresa->adresa; ?></textarea>                    
                        </td>
                    </tr>
                    <tr>
                        <td <?php check_field('oras'); ?> align="left">
                            <label for="oras"><?php echo LANG_ORAS . " " . CT_OBLIGATORIU; ?></label>
                        </td>
                        <td>
                            <input <?php echo show_tooltip(LANG_Oras_tooltip); ?> size="33" type="text" id="oras" name="oras" maxlength="<?php echo CT_MAX_ORAS_LENGTH; ?>" 
                                value="<?php echo $_SESSION['client']->adresa->oras; ?>"/>                    
                        </td>
                    </tr>
                    <tr>
                        <td <?php check_field('judet'); ?> align="left">
                            <label for="judet"><?php echo LANG_JUDET . " " . CT_OBLIGATORIU; ?></label>
                        </td>
                        <td>
                            <input <?php echo show_tooltip(LANG_JUDET_tooltip); ?> size="33" type="text" id="judet" name="judet" 
                                maxlength="<?php echo CT_MAX_JUDET_LENGTH; ?>" 
                                value="<?php echo $_SESSION['client']->adresa->judet; ?>"/>                    
                        </td>
                    </tr>
                    <tr>
                        <td <?php check_field('cod_postal'); ?> align="left">
                            <label for="cod_postal"><?php echo LANG_COD_POSTAL . " " . CT_OBLIGATORIU; ?></label>
                        </td>
                        <td>
                            <input <?php echo show_tooltip(LANG_cod_postal_tooltip); ?> size="33" type="text" id="cod_postal" name="cod_postal"
                                maxlength="<?php echo CT_MAX_COD_POSTAL_LENGTH; ?>" 
                                value="<?php echo $_SESSION['client']->adresa->cod_postal; ?>"/>                    
                        </td>
                    </tr>
                    <tr>
                        <td <?php check_field('tara'); ?> align="left">
                            <label for="tara"><?php echo LANG_TARA . " " . CT_OBLIGATORIU; ?></label>
                        </td>
                        <td>
                            <input <?php echo show_tooltip(LANG_Tara_tooltip); ?> size="33" type="text" id="tara" name="tara"
                                maxlength="<?php echo CT_MAX_TARA_LENGTH; ?>" 
                                value="<?php echo $_SESSION['client']->adresa->tara; ?>"/>                    
                        </td>
                    </tr>            
                </table>
            </td>           
        </tr>
        <tr>
        </tr>
        <tr>
             <td align="left" valign="top" colspan="5">
                <br />
                 <div id="td_conturi">
                 <?php                                      
                     render_cont_table($_SESSION['client'], false, $currencies, CT_CONT_CLIENT, 1); 
                 ?>
                 </div>
                 <div id="mainContainer">
                     <input type="button" onclick="javascript: arata_modal_cont(<?php echo CT_CONT_CLIENT; ?>, <?php echo DIM_Firma_Add_Cont_Latime ?>, <?php echo DIM_Firma_Add_Cont_Inaltime; ?>)" value="<?php echo LANG_CREATE_NEW_CONT; ?>" />
                 </div>    
                 <br />                             
              </td>
          </tr>
		<tr>
            <td colspan="5" align="center">
                <div class="container">
                    <?php
                        if (isLoggedIn())
                            buton_final_ok(LANG_Edit_Client, "post_client");
                        else
                            buton_final_ok(LANG_Add_Client, "post_client");
                        echo '&nbsp;&nbsp;';
                        if (isLoggedIn())
                            buton_final_cancel(LANG_CANCEL, "home.php");
                        else
                            buton_final_cancel(LANG_CANCEL, "index.php");
                    ?>
                </div>
			</td>
		</tr>
		</form>
	</table>
	</div>
<script type="text/javascript">
        var tip_abonament_sel = document.getElementById('tip_abonament');
        show_pret_per_month(tip_abonament_sel.options[tip_abonament_sel.selectedIndex].value);
</script>
<?php
    
    if (isLoggedIn()) html_footer();
    }
    unset($_SESSION['wrong_ids']);
?>
